﻿using System.Collections.Generic;
using Gizmox.WebGUI.Forms;

namespace CustomsNuke.Web.Controls
{
    /// <summary>
    /// </summary>
    public class CommunicationManager
    {
        #region 基本属性

        private readonly List<IPublisher> _publishers = new List<IPublisher>();
        private readonly List<ISubscriber> _subscribers = new List<ISubscriber>();

        /// <summary>
        /// </summary>
        public List<IPublisher> Publishers
        {
            get
            {
                return _publishers;
            }
        }

        /// <summary>
        /// </summary>
        public List<ISubscriber> Subscribers
        {
            get
            {
                return _subscribers;
            }
        }

        #endregion

        #region 新增方法

        private void Add(IPublisher publisher)
        {
            if (publisher != null)
            {
                _publishers.Add(publisher);
                foreach (var subscriber in _subscribers)
                {
                    publisher.Communicate += subscriber.HandleCommunicate;
                }
            }
        }

        private void Add(ISubscriber subscriber)
        {
            if (subscriber != null)
            {
                _subscribers.Add(subscriber);
                foreach (var publisher in _publishers)
                {
                    publisher.Communicate += subscriber.HandleCommunicate;
                }
            }
        }

        #endregion

        #region 移除方法

        private void Remove(IPublisher publisher)
        {
            if (publisher != null)
            {
                _publishers.Remove(publisher);
                foreach (var subscriber in _subscribers)
                {
                    publisher.Communicate -= subscriber.HandleCommunicate;
                }
            }
        }

        private void Remove(ISubscriber subscriber)
        {
            if (subscriber != null)
            {
                _subscribers.Remove(subscriber);
                foreach (var publisher in _publishers)
                {
                    publisher.Communicate -= subscriber.HandleCommunicate;
                }
            }
        }

        #endregion

        #region 加载与卸载

        /// <summary>
        /// </summary>
        /// <param name = "control"></param>
        public void Load(Control control)
        {
            if (control is IPublisher)
            {
                Add((IPublisher) control);
            }
            if (control is ISubscriber)
            {
                Add((ISubscriber) control);
            }
        }

        /// <summary>
        /// </summary>
        /// <param name = "control"></param>
        public void Unload(Control control)
        {
            if (control is IPublisher)
            {
                Remove((IPublisher) control);
            }
            if (control is ISubscriber)
            {
                Remove((ISubscriber) control);
            }
        }

        #endregion
    }
}